`include "ascon_define.v"

module `ASCON_HASH_TOP
(
    input                                        ascon_hash_clk_i,
    input                                        ascon_hash_rst_n_i,
//与interface相连
    input              [`AHB_BUS_ADDR_W - 1 : 0] haddr_i,
    input                           [4  - 1 : 0] hprot_i,
    input                           [1  - 1 : 0] hsel_i,
    input                           [3  - 1 : 0] hsize_i,
    input                           [2  - 1 : 0] htrans_i,
    input              [`AHB_BUS_DATA_W - 1 : 0] hwdata_i,
    input                           [1  - 1 : 0] hwrite_i,
    output             [`AHB_BUS_DATA_W - 1 : 0] hrdata_o,
    output                          [1  - 1 : 0] hready_o,
    output                          [2  - 1 : 0] hresp_o,
    output                          [1  - 1 : 0] intr_o
);

wire                       [`AHB_BUS_ADDR_W-1:0] if2tx_buf_raddr;
wire                       [`AHB_BUS_DATA_W-1:0] tx_buf2if_rdat;

wire                                             if2rx_buf_en;
wire                                             if2rx_buf_wen;
wire                       [`AHB_BUS_ADDR_W-1:0] if2rx_buf_waddr;
wire                       [`AHB_BUS_DATA_W-1:0] if2rx_buf_wdat;

wire                                             if2ctrl_regs_en;
wire                                             if2ctrl_regs_wen;
wire                     [`AHB_BUS_ADDR_W-1 : 0] if2ctrl_regs_addr;
wire                     [`AHB_BUS_DATA_W-1 : 0] if2ctrl_regs_wdata;
wire                     [`AHB_BUS_DATA_W-1 : 0] ctrl_regs2if_rdata;

wire                                             fsm2ctrl_regs_prot;
wire                                             fsm2ctrl_regs_fsm_sta_wen;
wire                                             fsm2ctrl_regs_fsm_en_wen;
wire                           [`CTRL_REG_W-1:0] fsm2ctrl_regs_w_sta;
wire                           [`CTRL_REG_W-1:0] fsm2ctrl_regs_w_en;
wire                           [`CTRL_REG_W-1:0] ctrl_regs2fsm_msg_block_num;
wire                           [`CTRL_REG_W-1:0] ctrl_regs2fsm_msg_addr;
wire                           [`CTRL_REG_W-1:0] ctrl_regs2fsm_hash_ctrl_sta;
wire                                             ctrl_regs2fsm_hash_ctrl_en;
wire                                             ctrl_regs2fsm_hash_ctrl_mode;
wire                           [`CTRL_REG_W-1:0] ctrl_regs2fsm_hash_h_addr;

wire                                             am2fsm_vld;
wire                                             fsm2am_en;
wire                                             fsm2am_vld;
wire                                             fsm2am_mode;
wire                                             fsm2am_first;
wire                                             fsm2am_last;

wire                           [`BUF_ADDR_W-1:0] fsm2rx_buf_addr;

wire                                             fsm2tx_buf_en;
wire                                             fsm2tx_buf_wen;
wire                           [`BUF_ADDR_W-1:0] fsm2tx_buf_addr;

wire                                             sh2fsm_vld;
wire                                             fsm2sh_en;
wire                                             fsm2sh_vld;
wire                                             fsm2sh_mode;
wire                                             fsm2sh_first;

wire                                  [`M_W-1:0] rx_buf2am_m;
wire                                  [`S_W-1:0] am2sh_s;
wire                                  [`H_W-1:0] sh2tx_buf_h;
`ASCON_HASH_AHB_IF
u_ascon_hash_ahb_if
    (
    //ahb总线信号
    .tai_clk_i                         (ascon_hash_clk_i                       ),
    .tai_rst_n_i                       (ascon_hash_rst_n_i                     ),
    .tai_haddr_i                       (haddr_i                                ),
    .tai_hprot_i                       (hprot_i                                ),
    .tai_hsel_i                        (hsel_i                                 ),
    .tai_hsize_i                       (hsize_i                                ),
    .tai_htrans_i                      (htrans_i                               ),
    .tai_hwdata_i                      (hwdata_i                               ),
    .tai_hwrite_i                      (hwrite_i                               ),
    .tai_hrdata_o                      (hrdata_o                               ),
    .tai_hready_o                      (hready_o                               ),
    .tai_hresp_o                       (hresp_o                                ),
    .tai_t2r_intr_o                    (intr_o                                 ),
    //tx buf信号
    .tx_buf_raddr_o                    (if2tx_buf_raddr                        ),
    .tx_buf_rdat_i                     (tx_buf2if_rdat                         ),

    //rx buf信号
    .rx_buf_en_o                       (if2rx_buf_en                           ),
    .rx_buf_wen_o                      (if2rx_buf_wen                          ),
    .rx_buf_waddr_o                    (if2rx_buf_waddr                        ),
    .rx_buf_wdat_o                     (if2rx_buf_wdat                         ),

    //ctrl regs信号
    .ctrl_regs_en_o                    (if2ctrl_regs_en                        ),
    .ctrl_regs_wen_o                   (if2ctrl_regs_wen                       ),
    .ctrl_regs_addr_o                  (if2ctrl_regs_addr                      ),
    .ctrl_regs_wdata_o                 (if2ctrl_regs_wdata                     ),
    .ctrl_regs_rdata_i                 (ctrl_regs2if_rdata                     )
    );

`RX_BUF_2_32B_64
u_rx_buf
    (
    .clk_i                             (ascon_hash_clk_i                       ),
    .rstn_i                            (ascon_hash_rst_n_i                     ),

    .en_i                              (if2rx_buf_en                           ),
    .wen_i                             (if2rx_buf_wen                          ),
    .waddr_i                           (if2rx_buf_waddr                        ),
    .wdata_i                           (if2rx_buf_wdat                         ),
    .raddr_i                           (fsm2rx_buf_addr                        ),
    .rdata_o                           (rx_buf2am_m                            )
    );

`TX_BUF_2_32B_64
u_tx_buf
    (
    .clk_i                             (ascon_hash_clk_i                       ),
    .rstn_i                            (ascon_hash_rst_n_i                     ),

    .en_i                              (fsm2tx_buf_en                          ),
    .wen_i                             (fsm2tx_buf_wen                         ),
    .waddr_i                           (fsm2tx_buf_addr                        ),
    .wdata_i                           (sh2tx_buf_h                            ),

    .raddr_i                           (if2tx_buf_raddr                        ),
    .rdata_o                           (tx_buf2if_rdat                         )
    );

`ASCON_HASH_CTRL_REGS
u_ascon_hash_ctrl_regs
(
    .ascon_hash_clk_i                  (ascon_hash_clk_i                       ),
    .ascon_hash_rst_n_i                (ascon_hash_rst_n_i                     ),
//总线访问信号
    .ascon_hash_ctrl_regs_en_i         (if2ctrl_regs_en                        ),
    .ascon_hash_ctrl_regs_wen_i        (if2ctrl_regs_wen                       ),
    .ascon_hash_ctrl_regs_addr_i       (if2ctrl_regs_addr                      ),
    .ascon_hash_ctrl_regs_wdata_i      (if2ctrl_regs_wdata                     ),
    .ascon_hash_ctrl_regs_rdata_o      (ctrl_regs2if_rdata                     ),
//状态机访问信号
    .ascon_hash_ctrl_regs_prot_i       (fsm2ctrl_regs_prot                     ),
    .ascon_hash_ctrl_regs_fsm_sta_wen_i(fsm2ctrl_regs_fsm_sta_wen              ),
    .ascon_hash_ctrl_regs_fsm_en_wen_i (fsm2ctrl_regs_fsm_en_wen               ),
    .ascon_hash_ctrl_regs_w_sta_i      (fsm2ctrl_regs_w_sta                    ),
    .ascon_hash_ctrl_regs_w_en_i       (fsm2ctrl_regs_w_en                     ),
//输出配置信号
    .ascon_hash_ctrl_regs_msg_block_num_o (ctrl_regs2fsm_msg_block_num         ),
    .ascon_hash_ctrl_regs_msg_addr_o      (ctrl_regs2fsm_msg_addr              ),
    .ascon_hash_ctrl_regs_hash_ctrl_sta_o (ctrl_regs2fsm_hash_ctrl_sta         ),
    .ascon_hash_ctrl_regs_hash_ctrl_en_o  (ctrl_regs2fsm_hash_ctrl_en          ),
    .ascon_hash_ctrl_regs_hash_ctrl_mode_o(ctrl_regs2fsm_hash_ctrl_mode        ),
    .ascon_hash_ctrl_regs_hash_h_addr_o   (ctrl_regs2fsm_hash_h_addr           )
);

`ASCON_HASH_CTRL
u_ascon_hash_ctrl
(
    .ascon_hash_clk_i                  (ascon_hash_clk_i                       ),
    .ascon_hash_rst_n_i                (ascon_hash_rst_n_i                     ),
//与ctrl regs连接
    .ascon_hash_ctrl_en_i              (ctrl_regs2fsm_hash_ctrl_en             ),
    .ascon_hash_ctrl_msg_block_num_i   (ctrl_regs2fsm_msg_block_num            ),
    .ascon_hash_ctrl_regs_msg_addr_i   (ctrl_regs2fsm_msg_addr                 ),
    .ascon_hash_ctrl_mode_i            (ctrl_regs2fsm_hash_ctrl_mode           ),
    .ascon_hash_ctrl_h_addr_i          (ctrl_regs2fsm_hash_h_addr              ),
    .ascon_hash_ctrl_prot_o            (fsm2ctrl_regs_prot                     ),
    .ascon_hash_ctrl_fsm_sta_wen_o     (fsm2ctrl_regs_fsm_sta_wen              ),
    .ascon_hash_ctrl_fsm_en_wen_o      (fsm2ctrl_regs_fsm_en_wen               ),
    .ascon_hash_ctrl_w_sta_o           (fsm2ctrl_regs_w_sta                    ),
    .ascon_hash_ctrl_w_en_o            (fsm2ctrl_regs_w_en                     ),
//与ascon_hash_am连接
    .ascon_hash_ctrl_am_vld_i          (am2fsm_vld                             ),
    .ascon_hash_ctrl_am_en_o           (fsm2am_en                              ),
    .ascon_hash_ctrl_am_vld_o          (fsm2am_vld                             ),
    .ascon_hash_ctrl_am_mode_o         (fsm2am_mode                            ),
    .ascon_hash_ctrl_am_first_o        (fsm2am_first                           ),
    .ascon_hash_ctrl_am_last_o         (fsm2am_last                            ),
//与rx buf连接
    .ascon_hash_ctrl_rx_buf_addr_o     (fsm2rx_buf_addr                        ),
//与tx buf连接
    .ascon_hash_ctrl_tx_buf_en_o       (fsm2tx_buf_en                          ),
    .ascon_hash_ctrl_tx_buf_wen_o      (fsm2tx_buf_wen                         ),
    .ascon_hash_ctrl_tx_buf_addr_o     (fsm2tx_buf_addr                        ),
//与ascon_hash_sh连接
    .ascon_hash_ctrl_sh_vld_i          (sh2fsm_vld                             ),
    .ascon_hash_ctrl_sh_en_o           (fsm2sh_en                              ),
    .ascon_hash_ctrl_sh_vld_o          (fsm2sh_vld                             ),
    .ascon_hash_ctrl_sh_mode_o         (fsm2sh_mode                            ),
    .ascon_hash_ctrl_sh_first_o        (fsm2sh_first                           )
);

`ASCON_HASH_AM
u_ascon_hash_am
(
    .ascon_hash_clk_i                  (ascon_hash_clk_i                       ),
    .ascon_hash_rst_n_i                (ascon_hash_rst_n_i                     ),

    .ascon_hash_am_en_i                (fsm2am_en                              ),
    .ascon_hash_am_vld_i               (fsm2am_vld                             ),
    .ascon_hash_am_mode_i              (fsm2am_mode                            ),
    .ascon_hash_am_first_i             (fsm2am_first                           ),
    .ascon_hash_am_last_i              (fsm2am_last                            ),
    .ascon_hash_am_m_i                 (rx_buf2am_m                            ),

    .ascon_hash_am_s_o                 (am2sh_s                                ),
    .ascon_hash_am_vld_o               (am2fsm_vld                             )
);

`ASCON_HASH_SH
u_ascon_hash_sh
(
    .ascon_hash_clk_i                  (ascon_hash_clk_i                       ),
    .ascon_hash_rst_n_i                (ascon_hash_rst_n_i                     ),

    .ascon_hash_sh_en_i                (fsm2sh_en                              ),
    .ascon_hash_sh_vld_i               (fsm2sh_vld                             ),
    .ascon_hash_sh_mode_i              (fsm2sh_mode                            ),
    .ascon_hash_sh_first_i             (fsm2sh_first                           ),
    .ascon_hash_sh_s_i                 (am2sh_s                                ),

    .ascon_hash_sh_h_o                 (sh2tx_buf_h                            ),
    .ascon_hash_sh_vld_o               (sh2fsm_vld                             )
);

endmodule
